package DAOs;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import classes.Usuario;

public class UsuarioDAO {
	
	public void registrar(String nombre, String apellido,String alias,String pass,Date fechaA, Date fechaM)  {
		Connection conn;
		try {
			conn = new DBConnector().getConnection();
			try{
				
					
					PreparedStatement statement = conn.prepareStatement("INSERT INTO usuarios (nombre,apellido,alias,password,fechaAlta,fechaModificacion) VALUES (?,?,?,?,?,?)");

					statement.setString(1, nombre);
					statement.setString(2, apellido);
					statement.setString(3, alias);
					//statement.setString(4, new String(MessageDigest.getInstance("MD5").digest("hola".getBytes())));
					statement.setString(4, pass);
					//statement.setDate(5, new Date(new java.util.Date().getTime()));
					statement.setDate(5, fechaA);
					//statement.setDate(6, new Date(new java.util.Date().getTime()));
					statement.setDate(6, fechaM);
					
					int registrosModificados = statement.executeUpdate();
					//System.out.println("Lineas Modificadas" + registrosModificados);
					//(System.out.println("Usuario registrado!");

			}catch (SQLException e){
				
			}finally{
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public void borrar(String alias){		
		try{
			Connection conn = new DBConnector().getConnection();
			try{
				PreparedStatement prepareStatement = conn.prepareStatement("DELETE FROM usuarios WHERE alias`= ?");
				prepareStatement.setString(1, alias);
				ResultSet consultaExiste =  prepareStatement.executeQuery();
				//System.out.println("Usuario borrado!");
				}catch(SQLException e){
				
			}finally{
				conn.close();
			}			
		}catch(SQLException e){
			
		}
		
	}
	
	public boolean existeUsuario(String alias){
		boolean result = false;
		try{
			Connection conn = new DBConnector().getConnection();
			try{
				PreparedStatement prepareStatement = conn.prepareStatement("select alias from usuarios where alias = ?");
				prepareStatement.setString(1, alias);
				ResultSet consultaExiste =  prepareStatement.executeQuery();
				
				if (consultaExiste.next()){
					result = true;
					
					}else{
					
					}
				}catch(SQLException e){
				
			}finally{
				conn.close();
			}			
		}catch(SQLException e){
			
		}
		return result;
	}

	public Usuario getUsuario(String alias)  {
		
		Connection conn;
		try {
			conn = new DBConnector().getConnection();
			try {
				PreparedStatement prepareStatement = conn.prepareStatement("select * from usuarios where alias = ?");
				prepareStatement.setString(1, alias);
				ResultSet cons =  prepareStatement.executeQuery();		
				cons.next();
				
				Usuario u = new Usuario(cons.getString("nombre"), cons.getString("apellido"),cons.getString("alias"),cons.getString("password"),cons.getDate("fechaAlta"),cons.getDate("fechaModificacion"));
				
				return u;
			} catch (SQLException e) {
				throw new DBException(e);
			} finally {
				conn.close();
			}
		} catch (SQLException s) {
			throw new DBException(s);
		}
		
	}

	public void cambiarPassword(String alias, String password) {
		Connection conn;
		try {
			conn = new DBConnector().getConnection();
			try {
				PreparedStatement prepareStatement = conn.prepareStatement("UPDATE usuarios SET password = ? WHERE alias = ?");
				prepareStatement.setString(1, password);
				prepareStatement.setString(2, alias);
				prepareStatement.executeUpdate();
			} catch (SQLException e) {
				throw new DBException(e);
			} finally {
				conn.close();
			}
		} catch (SQLException s) {
			throw new DBException(s);
		};
	}
}
